import 'package:flutter/material.dart';
import 'package:flutter_module/page/service_provider_edit/widgets/service_provider_banner.dart';

/// Create by kyluzoi
/// Date: 2021-08-25
///
/// Description:

class ScrollableBannerContainer extends StatefulWidget {
  final ScrollController scrollController;
  final List<String> banners;

  const ScrollableBannerContainer(
      {Key? key, required this.scrollController, required this.banners})
      : super(key: key);

  @override
  _ScrollableBannerContainerState createState() =>
      _ScrollableBannerContainerState();
}

class _ScrollableBannerContainerState extends State<ScrollableBannerContainer> {
  double _bannerOpacity = 1.0;
  double _bannerHeight = 200;

  _scrollListener() {
    double opacity =
        (_bannerHeight - widget.scrollController.offset) / _bannerHeight;
    opacity = opacity > 1
        ? 1
        : opacity < 0
        ? 0
        : opacity;
    if (opacity != _bannerOpacity) {
      setState(() {
        _bannerOpacity = opacity;
      });
    }
  }

  @override
  void initState() {
    widget.scrollController.addListener(_scrollListener);
    super.initState();
  }

  @override
  void dispose() {
    widget.scrollController.removeListener(_scrollListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Opacity(
      opacity: _bannerOpacity,
      child: SizedBox(
        width: double.infinity,
        height: _bannerHeight,
        child: ServiceProviderBanner(
          fit: BoxFit.cover,
          showEdit: false,
          bannerImgUrls: widget.banners,
        ),
      ),
    );
  }
}